[Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8

function Get-ActiveNetAdapter {
    $adapters = Get-NetAdapter
    $activeAdapter = $adapters | Where-Object { $_.Status -eq "Up" }
    return $activeAdapter
}

$activeAdapter = Get-ActiveNetAdapter

if ($activeAdapter) {
    Write-Output "current netcard:"
    Write-Output $activeAdapter.Name
} else {
    Write-Output "Can not find network,Unknown netcard!"
}

# 启用和禁用网卡的通用函数
function Set-NetAdapterStateByName {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Name,

        [Parameter(Mandatory=$true)]
        [string]$Action
    )
    $adapter = Get-NetAdapter -Name $Name
    if ($adapter) {
        if ($Action -eq "Disable") {
            Disable-NetAdapter -Name $Name -Confirm:$false
            Write-Output "netcard $Name has disabled"
        } elseif ($Action -eq "Enable") {
            Enable-NetAdapter -Name $Name -Confirm:$false
            Write-Output "netcard $Name has enabled"
        } else {
            Write-Output "Unknown action: $Action"
        }
    } else {
        Write-Output "$Action failed, can not find: $Name"
    }
}

Set-NetAdapterStateByName -Name "net1" -Action "Disable"
Set-NetAdapterStateByName -Name "net2" -Action "Enable"

function Get-CurrentAdapterSpeed {
    param(
        [Parameter(Mandatory=$true)]
        [string]$adapterName,

        [Parameter(Mandatory=$false)]
        [int]$interval = 2
    )
    $startTime = Get-Date
    $endTime = $startTime + (New-TimeSpan -Seconds $interval)
    $startStats = Get-NetAdapterStatistics -Name $adapterName
    Start-Sleep -Seconds $interval
    $endStats = Get-NetAdapterStatistics -Name $adapterName
    $totalReceivedBytes = $endStats.ReceivedBytes - $startStats.ReceivedBytes
    $totalSentBytes = $endStats.SentBytes - $startStats.SentBytes
    $averageReceivedSpeed = [Math]::Round($totalReceivedBytes / ($interval) / 1024)
    $averageSentSpeed = [Math]::Round($totalSentBytes / ($interval) / 1024)
    return @{
        ReceivedSpeed = $averageReceivedSpeed;
        SentSpeed = $averageSentSpeed;
    }
}

$speedResult = Get-CurrentAdapterSpeed -adapterName "net2"
Write-Output $speedResult

# 初始化参数
$thresholdSpeed = 1024  # 1M/s in kb/s
$speedCheckDuration = 3
$maxLowSpeedDurationCount = 3  # 15 seconds at 5s interval
$speedBelowThresholdCount = 0
$state = "WaitingToReceiveFile"

# 切换网卡的逻辑
function Switch-AdaptersIfNecessary {
    $currentAdapterName = "net2"
    $state = "WaitingToReceiveFile"
    $speedBelowThresholdCount = 0

    while ($true) {
        $currentAdapterSpeed = Get-CurrentAdapterSpeed -adapterName $currentAdapterName
        $receivedSpeed = $currentAdapterSpeed.ReceivedSpeed
        $sentSpeed = $currentAdapterSpeed.SentSpeed

        Write-Host "$(Get-Date) - Current adapter: $currentAdapterName, received speed: $receivedSpeed kb/s, sent speed: $sentSpeed kb/s"

        switch ($state) {
            "WaitingToReceiveFile" {
                if ($receivedSpeed -gt $thresholdSpeed) {
                    Write-Host "Switching to ReceivingFile state"
                    $state = "ReceivingFile"
                    $speedBelowThresholdCount = 0
                }
            }
            "ReceivingFile" {
                if ($receivedSpeed -lt $thresholdSpeed) {
                    $speedBelowThresholdCount++
                    Write-Host "$(Get-Date) - Speed below threshold. Count: $speedBelowThresholdCount"
                } else {
                    $speedBelowThresholdCount = 0
                }

                if ($speedBelowThresholdCount -ge $maxLowSpeedDurationCount) {
                    Write-Host "File receiving completed. Switching to FileReceived state."
                    $currentAdapterName = "net1"
                    Set-NetAdapterStateByName -Name "net2" -Action "Disable"
                    Set-NetAdapterStateByName -Name "net1" -Action "Enable"
                    Start-Sleep -Seconds 5
                    $state = "WaitingToUploadFile"
                }
            }
            "WaitingToUploadFile" {
                if ($sentSpeed -gt $thresholdSpeed) {
                    Write-Host "Switching to UploadingFile state"
                    $state = "UploadingFile"
                    $speedBelowThresholdCount = 0
                }
            }
            "UploadingFile" {
                if ($sentSpeed -lt $thresholdSpeed) {
                    $speedBelowThresholdCount++
                    Write-Host "$(Get-Date) - Speed below threshold. Count: $speedBelowThresholdCount"
                } else {
                    $speedBelowThresholdCount = 0
                }

                if ($speedBelowThresholdCount -ge $maxLowSpeedDurationCount) {
                    Write-Host "File uploading completed. Switching to FileUploaded state."
                    $currentAdapterName = "net2"
                    Set-NetAdapterStateByName -Name "net1" -Action "Disable"
                    Set-NetAdapterStateByName -Name "net2" -Action "Enable"
                    Start-Sleep -Seconds 5
                    $state = "WaitingToReceiveFile"
                }
            }
        }
        Start-Sleep -Seconds $speedCheckDuration
    }
}

Switch-AdaptersIfNecessary
